package net.xdclass.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.xdclass.enums.AddressStatusEnum;
import net.xdclass.interceptor.LoginInterceptor;
import net.xdclass.mapper.AddressMapper;
import net.xdclass.model.AddressDO;
import net.xdclass.model.LoginUser;
import net.xdclass.request.AddAddressRequest;
import net.xdclass.service.AddressService;
import net.xdclass.util.JsonData;
import net.xdclass.vo.AddressVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Classname AddressServiceImpl
 * @Description TODO
 * @Date 2022/4/4 17:34
 * @Created by sunhao
 */
@Service
public class AddressServiceImpl implements AddressService {
  @Autowired
  private AddressMapper addressMapper;

  @Override
  public AddressVo detail(String addressId) {
    LoginUser loginUser = LoginInterceptor.threadLocal.get();

    AddressDO addressDO = addressMapper.selectOne(new QueryWrapper<AddressDO>().eq("id", addressId).eq("user_id",loginUser.getId()));
    if(addressDO == null){
      throw null;
    }
    AddressVo addressVo = new AddressVo();
    BeanUtils.copyProperties(addressDO,addressVo);
    return addressVo;
  }

  @Transactional(rollbackFor = Exception.class)
  @Override
  public JsonData saveUserAddress(AddAddressRequest addAddressRequest) {
    LoginUser loginUser = LoginInterceptor.threadLocal.get();
    AddressDO addressDO = new AddressDO();
    addressDO.setUserId(loginUser.getId());
    addressDO.setCreateTime(new Date());
    BeanUtils.copyProperties(addAddressRequest, addressDO);
    if (addAddressRequest.getDefaultStatus() == AddressStatusEnum.DEFAULT_STATUS.getStatus()) {
      //设置的是默认收货地址 需要将原来的默认收货地址修改为0
      AddressDO defaultAddress = addressMapper.selectOne(new QueryWrapper<AddressDO>().eq("user_id", loginUser.getId()).eq("default_status", AddressStatusEnum.DEFAULT_STATUS.getStatus()));
      if (defaultAddress != null) {
        defaultAddress.setDefaultStatus(AddressStatusEnum.COMMON_STATUS.getStatus());
        addressMapper.updateById(defaultAddress);
      }
    }
    int rows = addressMapper.insert(addressDO);
    return rows > 0 ? JsonData.buildSuccess() : JsonData.buildError("保存失败");
  }

  @Transactional(rollbackFor = Exception.class)
  @Override
  public int delete(String addressId) {
    LoginUser loginUser = LoginInterceptor.threadLocal.get();
    return  addressMapper.delete(new QueryWrapper<AddressDO>().eq("id",addressId).eq("user_id",loginUser.getId()));
  }

  @Override
  public List<AddressVo> findUserAllAddress() {
    LoginUser loginUser = LoginInterceptor.threadLocal.get();
    List<AddressDO> addressDOList = addressMapper.selectList(new QueryWrapper<AddressDO>().eq("user_id", loginUser.getId()));
    return  addressDOList.stream().map(addressDO->{
      AddressVo addressVO = new AddressVo();
      BeanUtils.copyProperties(addressDO, addressVO);
      return addressVO;
    }).collect(Collectors.toList());
  }
}
